<template>
  <div class="main">
    <el-button type="primary" @click="dialogVisible = true">添加问题咨询<i class="el-icon-plus el-icon--right"></i></el-button>
    <el-dialog
        title="添加问题咨询"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <router-link to="/expertSelect" class="link">查询专家详细信息请点击此处</router-link>
        <el-form-item label="专家选择" prop="eid">
          <el-select v-model="ruleForm.eid" filterable placeholder="请选择专家">
            <el-option
                v-for="item in allExpert"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
          <span v-show="ruleForm.eid !== null" style="display: inline;color: red">专家id：{{ruleForm.eid}}</span>
        </el-form-item>
        <el-form-item label="问题描述" prop="question">
          <el-input type="textarea" v-model="ruleForm.question"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {mapActions, mapState} from "vuex";
import request from "@/utils/request";

export default {
  name: "AddQuestionForm",
  data() {
    return {
      dialogVisible: false,
      value: '',
      ruleForm: {
        fid:null,
        eid: null,
        question: '',
        date:''
      },
      rules: {
        eid: [
          { required: true, message: '请选择专家', trigger: 'change' }
        ],
        question: [
          { required: true, message: '请填写问题描述', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    handleClose(done) {
      this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
    },
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          this.dialogVisible = false

          this.ruleForm.date = new Date().Format("yyyy-MM-dd hh:mm:ss");
          await this.findFarmerByUid();
          this.ruleForm.fid = this.loginFarmer.id
          await request.post("/crop/question/addQuestion",this.ruleForm).then(
              response => {
                this.$message.success("问题提交成功！")
                this.findAllUnsolved(this.ruleForm.fid)
              },
              error => {
                console.log(error)
              }
          )
        } else {
          console.log('error submit!!');
          this.$message.error("上传失败!")
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    ...mapActions('expert',['findAllExpert']),
    ...mapActions('question',['findAllUnsolved','findAllSolved']),
    ...mapActions('farmer',['findFarmerByUid']),
  },
  computed:{
    ...mapState('expert',['allExpert']),
    ...mapState('user',['loginUser']),
    ...mapState('farmer',['loginFarmer']),
  },
  async created() {
    await this.findAllExpert()
  }
}

Date.prototype.Format = function (fmt) { // author: meizz
  let o = {
    "M+": this.getMonth() + 1, // 月份
    "d+": this.getDate(), // 日
    "h+": this.getHours(), // 小时
    "m+": this.getMinutes(), // 分
    "s+": this.getSeconds(), // 秒
    "q+": Math.floor((this.getMonth() + 3) / 3), // 季度
    "S": this.getMilliseconds() // 毫秒
  };
  if (/(y+)/.test(fmt))
    fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (let k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}
</script>

<style scoped>
.main{
  width: 100%;
  margin: 20px;
}

.link{
  position: absolute;
  top: 50px;
  margin-bottom: 30px;
}

.link:hover{
  color: dodgerblue;
  border-bottom: 1px solid dodgerblue;
}
</style>